home *** CD-ROM | disk | FTP | other *** search
- var WiseStampUtils = function() {
- var pub = {};
-
- var DEBUG_MODE = null;
- var m_consoleService = null;
-
- var m_nativeJson = null;
- var m_userCode = null;
-
- pub.getDebugMode = function()
- {
- if (DEBUG_MODE == null)
- DEBUG_MODE = WiseStampPrefs.getBoolPref("debug",false);
-
- return DEBUG_MODE;
- }
-
- pub.getRandomInt = function(min, max)
- {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
-
- function generateCode(length)
- {
- /* list all possible characters, similar looking characters and vowels have been removed */
- var possible = '23456789bcdfghjkmnpqrstvwxyz';
- var code = '';
-
- for (var i = 0; i < length; i++)
- code += possible[pub.getRandomInt(0, possible.length-1)];
-
- return code;
- }
-
- // public functions
- pub.getUserCode = function()
- {
- if (m_userCode == null)
- {
- // create a random code if not created
- m_userCode = WiseStampPrefs.getCharPref("userCode");
- if (m_userCode == "") // failed to read from config
- {
- m_userCode = generateCode(16); // 16 chars log random number
- WiseStampPrefs.PREFS.setCharPref("userCode", m_userCode);
- }
- }
-
- return m_userCode;
- }
-
- pub.setUserCode = function(code)
- {
- m_userCode = code;
- }
-
- pub.getVersion = function()
- {
- var extensionManager = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
- var addon = extensionManager.getItemForID("wisestamp@wisestamp.com");
- return addon.version;
- }
-
- pub.getAppType = function()
- {
- const FIREFOX_ID = "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}";
- const THUNDERBIRD_ID = "{3550f703-e582-4d05-9a08-453d09bdfdc6}";
-
- var appInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
- if (appInfo.ID == THUNDERBIRD_ID)
- return "Thunderbird";
- else
- return "Firefox";
- }
-
- pub.getAppVersion = function()
- {
- var appInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
- return appInfo.version;
- }
-
- pub.getSignaturesFromPref = function()
- {
- //return pub.PREFS.getCharPref("signatures").split(",");
- var ret = null;
- try {
- ret = WiseStampPrefs.PREFS.getComplexValue("signatures",Components.interfaces.nsISupportsString).data.split(",");
- } catch(e)
- {
- alert(e);
- }
-
- return ret;
- }
-
- pub.setSignaturesToPref = function(signatures)
- {
- var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
- str.data = signatures.join(",");
- WiseStampPrefs.PREFS.setComplexValue("signatures", Components.interfaces.nsISupportsString, str);
- }
-
- pub.log = function(msg)
- {
- if (pub.getDebugMode() != true)
- return;
-
- if (this.m_consoleService == null)
- this.m_consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
-
- msg = "WISESTAMP: " + msg;
- this.m_consoleService.logStringMessage(msg)
- }
-
- pub.prompt = function(title,default_val,text)
- {
- var Cc = Components.classes;
- var Ci = Components.interfaces;
- var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
- var check = {value: null};
- var input = {value: default_val};
- var result = prompts.prompt(window,
- WisestampSignatureFactory.strings.GetStringFromName(title),
- WisestampSignatureFactory.strings.GetStringFromName(text), input, null, check);
-
- if (result == false)
- return null;
-
- if (input.value == default_val)
- return "";
-
- return input.value;
- }
-
- pub.confirm = function(title,text,button1,button2,button3)
- {
- var Cc = Components.classes;
- var Ci = Components.interfaces;
- var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
- var check = {value: null};
-
- var flags =
- prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_IS_STRING +
- prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_IS_STRING;
-
- if (button3 != undefined)
- flags += prompts.BUTTON_POS_2 * prompts.BUTTON_TITLE_IS_STRING;
-
- var selected = prompts.confirmEx(
- window,
- WisestampSignatureFactory.strings.GetStringFromName(title),
- WisestampSignatureFactory.strings.GetStringFromName(text),
- flags,
- button1 == undefined ? "" : WisestampSignatureFactory.strings.GetStringFromName(button1),
- button2 == undefined ? "" : WisestampSignatureFactory.strings.GetStringFromName(button2),
- button3 == undefined ? "" : WisestampSignatureFactory.strings.GetStringFromName(button3),
- null, check);
-
- return selected;
- }
-
- pub.alert = function(text,title)
- {
- if (title == null)
- title = "application_name";
-
- pub.alert2(WisestampSignatureFactory.strings.GetStringFromName(text),WisestampSignatureFactory.strings.GetStringFromName(title));
- }
-
- pub.alert2 = function(text,title)
- {
- if (title == null)
- title = WisestampSignatureFactory.strings.GetStringFromName("application_name");
-
- var Cc = Components.classes;
- var Ci = Components.interfaces;
- var prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
- prompts.alert(window, title, text);
- }
-
- pub.openSite = function (aURL)
- {
- WisestampGetAppObject().openSite(aURL);
- }
-
- pub.md5 = function(string)
- {
- var converter =
- Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
- createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
-
- // we use UTF-8 here, you can choose other encodings.
- converter.charset = "UTF-8";
-
- // result is an out parameter, result.value will contain the array length
- var result = {};
-
- // data is an array of bytes
- var data = converter.convertToByteArray(string, result);
-
- var ch = Components.classes["@mozilla.org/security/hash;1"].createInstance(Components.interfaces.nsICryptoHash);
- ch.init(ch.MD5);
- ch.update(data, data.length);
-
- var hash = ch.finish(false);
-
- // return the two-digit hexadecimal code for a byte
- function toHexString(charCode)
- {
- return ("0" + charCode.toString(16)).slice(-2);
- }
-
- // convert the binary hash data to a hex string.
- var md5string = [toHexString(hash.charCodeAt(i)) for (i in hash)].join("");
- return md5string;
- }
-
- pub.exportLog = function(file)
- {
- // file is nsIFile, data is a string
- var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
-
- // use 0x02 | 0x10 to open file for appending.
- foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
-
- // if you are sure there will never ever be any non-ascii text in data you can
- // also call foStream.writeData directly
- var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);
- converter.init(foStream, "UTF-8", 0, 0);
-
- var messages = {};
- var count = {};
- this.m_consoleService.getMessageArray(messages,count);
- for (var i = 0; i < count.value; i++)
- {
- try
- {
- var message = messages.value[i];
- var text = message.message;
-
- if (message.sourceName)
- text += "\n\rSource: " + message.sourceName + ", line: " + message.lineNumber;
-
- if (text.toLowerCase().indexOf('wisestamp') == -1)
- continue;
-
- converter.writeString(text + '\r\n');
-
- } catch(e)
- {
- WiseStampUtils.log("utils.js :: exportLog :: exception caught = " + e);
- }
- }
-
- converter.close(); // this closes foStream
- }
-
- pub.browseFile = function(openFile,type,typeExt)
- {
- var nsIFilePicker = Components.interfaces.nsIFilePicker;
- var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
-
- var mode = (openFile == true) ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave;
- fp.init(window, "Select a File", mode);
- if (type != undefined)
- {
- fp.appendFilter(type + " (*."+typeExt+")", "*."+typeExt);
- fp.defaultExtension = typeExt;
- }
-
- var res = fp.show();
- if (res == nsIFilePicker.returnOK)
- return fp.file;
-
- return null;
- }
-
- pub.strip_tags = function(str, allowed_tags)
- {
- var key = '', allowed = false;
- var matches = [];
- var allowed_array = [];
- var allowed_tag = '';
- var i = 0;
- var k = '';
- var html = '';
-
- var replacer = function (search, replace, str) {
- return str.split(search).join(replace);
- };
-
- // Build allowes tags associative array
- if (allowed_tags) {
- allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
- }
-
- str += '';
-
- // Match tags
- matches = str.match(/(<\/?[\S][^>]*>)/gi);
-
- // Go through all HTML tags
- for (key in matches) {
- if (isNaN(key)) {
- // IE7 Hack
- continue;
- }
-
- // Save HTML tag
- html = matches[key].toString();
-
- // Is tag not in allowed list? Remove from str!
- allowed = false;
-
- // Go through all allowed tags
- for (k in allowed_array) {
- // Init
- allowed_tag = allowed_array[k];
- i = -1;
-
- if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
- if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
- if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag) ;}
-
- // Determine
- if (i == 0) {
- allowed = true;
- break;
- }
- }
-
- if (!allowed) {
- str = replacer(html, "", str); // Custom replace. No regexing
- }
- }
-
- return str;
- };
-
- ///////////////////////////////////////////////////////
-
- function getJSON()
- {
- if (this.m_nativeJson == null)
- {
- try {
- var Ci = Components.interfaces;
- var Cc = Components.classes;
- this.m_nativeJson = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
-
- } catch (e) { // TB2 doesn't support this interface
- this.m_nativeJson = {decode:function(){return '';},encode:function(){return '';}};
- }
- }
-
- return this.m_nativeJson;
- }
-
- pub.parseSerialized = function(serialized)
- {
- return getJSON().decode(serialized);
- }
-
- pub.serialize = function(object)
- {
- return getJSON().encode(object);
- }
-
- ///////////////////////////////////////////////////////
-
- function getLoginInfo(username)
- {
- var hostname = 'chrome://wisestamp';
- var loginManager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager);
- var logins = loginManager.findLogins({}, hostname, "", "Login");
-
- // Find user from returned array of nsILoginInfo objects
- for (var i = 0; i < logins.length; i++)
- {
- WiseStampUtils.log("[utils.js::getLoginInfo] username/password = " + logins[i].username + '/' + logins[i].password);
-
- if (logins[i].username == username)
- return logins[i];
- }
-
- return null;
- }
-
- pub.storePassword = function(username,password)
- {
- var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",Components.interfaces.nsILoginInfo,"init");
- var loginInfo = new nsLoginInfo('chrome://wisestamp',"","Login",username,password,"","");
-
- var loginManager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager);
-
- var oldLoginInfo = getLoginInfo(username);
- WiseStampUtils.log("[utils.js::storePassword] username/password = " + username + '/' + oldLoginInfo.password);
- if (oldLoginInfo == null)
- loginManager.addLogin(loginInfo);
- else
- loginManager.modifyLogin(oldLoginInfo, loginInfo);
- }
-
- pub.getPassword = function(username)
- {
- var loginInfo = getLoginInfo(username);
- if (loginInfo != null)
- return loginInfo.password;
-
- return null;
- }
-
- return pub;
- }();
-
-
- /*
- // Call stack code
- showCallStack: function(){
- var f=this.showCallStack,result="Call stack:\n\n";
- alert(f.name);
- alert(f.arguments.callee);
- while((f=f.caller)!==null)
- {
- var match = f.toString().match(/^function \((.*)\)/);
- if (match == null)
- {
- alert(f.toString());
- break;
- }
-
- result += "F:" + match[0] + "\n";
- result += "A:" + this.parseArguments(f.arguments) + "\n";
- result += "\n";
- }
- alert(result);
- },
-
- parseArguments: function(a){
- var result=[];
-
- for(var i=0; i<a.length; i++){
- if ('string' == typeof a[i])
- result.push("\"" + a[i] + "\"");
- else
- result.push(a[i]);
- }
-
- return "(" + result.join(", ") + ")";
- },
- */